package com.example.task3.service;

import com.example.task3.domain.Cake;

public class ShoppingItem {
    Cake cake;
    int quantity;

    public ShoppingItem(Cake cake, int quantity) {
        this.cake = cake;
        this.quantity = quantity;
    }

    public Cake getCake() {
        return cake;
    }

    public void setCake(Cake cake) {
        this.cake = cake;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    /**
     * 商品小计
     * @return
     */
    public double getSubTotal(){
        return cake.getPrice()*quantity;
    }

    /**
     * 减少数量
     * @param num
     */
    public void deQuantity(int num){
        if(num<0) {
            throw new IllegalArgumentException("减少数量不能小于0");
        }
        if(quantity-num<=0){
            throw new IllegalArgumentException("减少后的数量不能小于或者等于0");
        }
        quantity-=num;
    }

    /**
     * 增加数量
     * @param num
     */
    public void inQuantity(int num){
        if(num<0) {
            throw new IllegalArgumentException("增加的数量必须大于0");
        }
        quantity+=num;
    }
}
